//
//  LXTextView.m
//  微了个小博
//
//  Created by imera on 15/11/26.
//  Copyright (c) 2015年 imera. All rights reserved.
//

#import "LXTextView.h"
#import "UILabel+LX.h"

@interface LXTextView()

@property (nonatomic, weak) UILabel *placeHolderLabel;

@end

@implementation LXTextView

- (void)setPlaceHolder:(NSString *)placeHolder
{
    _placeHolder = [placeHolder copy];
    
    self.placeHolderLabel.text = placeHolder;
    
    //设置placeHolderLabel的frame
    CGFloat marginX = 5;
    CGFloat marginY = 8;
    CGFloat maxWith = self.frame.size.width - marginX * 2;
    CGFloat maxHeight = self.frame.size.height - marginY * 2;
    CGSize fontSize = [self.placeHolderLabel sizeWithString:placeHolder font:LXComposeFont maxWith:maxWith maxHeight:maxHeight];
    self.placeHolderLabel.frame = CGRectMake(marginX, marginY, fontSize.width, fontSize.height);
}

- (void)setPlaceHolderColor:(UIColor *)placeHolderColor
{
    _placeHolderColor = placeHolderColor;
    
    self.placeHolderLabel.textColor = placeHolderColor;
}

- (void)setPlaceHolderFont:(UIFont *)placeHolderFont
{
    _placeHolderFont = placeHolderFont;
    
    self.placeHolderLabel.font = placeHolderFont;
    self.placeHolder = self.placeHolderLabel.text;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        //添加提示文字
        UILabel *placeHolderLabel = [[UILabel alloc]init];
        placeHolderLabel.hidden = NO;
        placeHolderLabel.textColor = [UIColor lightGrayColor];
        placeHolderLabel.backgroundColor = [UIColor clearColor];
        placeHolderLabel.numberOfLines = 0;
        placeHolderLabel.font = LXComposeFont;
        [self insertSubview:placeHolderLabel atIndex:0];
        self.placeHolderLabel = placeHolderLabel;
        
        //添加文字改变通知
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:self];
    }
    return self;
}

- (void)textChange
{
    self.placeHolderLabel.hidden = (self.text.length != 0);
}

@end